package dcds2;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.sql.*;

public class ViewPatientWindow extends javax.swing.JFrame {
         
    public int sPatient;
    
     public String[] months = {"January","February", "March","April", "May", "June", "July", "August", "September"
                        , "October", "November", "December"};
     public String[] days31 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30", "31"};
    
     public String[] days30 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30"};
     public String[] days28 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28"};
     public String[] days29 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29"};
    
    public ViewPatientWindow(int patientID) {
        sPatient = patientID;
        initComponents();
        
        groupGender.add(radiobtnMale);
        groupGender.add(radiobtnFemale);
        setLocationRelativeTo(null);
        
         try{
         ConnectionFactory myFactory = ConnectionFactory.getFactory();
         Connection conn = myFactory.getConnection();
         boolean result = false;
         
         PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.patient WHERE patientID=" +sPatient);

         rs = pstmt.executeQuery();
        
        while(rs.next()){
            
            int pI = rs.getInt("patientID");
            String fN = rs.getString("pFirstName");
            txtFirstName.setText(fN);
            String mN = rs.getString("pMiddleName");
            txtMiddleName.setText(mN);
            String lN = rs.getString("pLastName");
            txtLastName.setText(lN);
            String A = rs.getString("pAddress");
            txtAddress.setText(A);
            String C = rs.getString("pContactNo");
            txtContactNo.setText(C);
            Object month = rs.getObject("pBirthMonth");
            comboMonth.setSelectedItem(month);
            String day = rs.getString("pBirthDay");
            comboDay.setSelectedItem(day);
            String year = rs.getString("pBirthYear");
            txtYear.setText(year);
            String gender = rs.getString("pGender");
            
            if(gender.equals("Male")){
                radiobtnMale.setSelected(true);
                radiobtnFemale.setSelected(false);
            }else {
                radiobtnFemale.setSelected(true);
                radiobtnMale.setSelected(false);
            }
        
            
            String civil = rs.getString("pCivilStat");
            comboCivil.setSelectedItem(civil); 
        }
        
    conn.close();
    result = true;
    }catch(Exception e){
        e.printStackTrace();
         }     
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupGender = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtFirstName = new javax.swing.JTextField();
        txtMiddleName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        txtContactNo = new javax.swing.JTextField();
        comboMonth = new javax.swing.JComboBox();
        txtYear = new javax.swing.JTextField();
        comboDay = new javax.swing.JComboBox();
        radiobtnMale = new javax.swing.JRadioButton();
        radiobtnFemale = new javax.swing.JRadioButton();
        comboCivil = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ToggleEdit = new javax.swing.JToggleButton();
        btnConfirm = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();
        btnPatientLog = new javax.swing.JButton();
        txtAddress = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - View Patient");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setEnabled(false);

        jLabel1.setFont(new java.awt.Font("Century", 1, 12));
        jLabel1.setText("PERSONAL INFORMATION");

        jLabel2.setFont(new java.awt.Font("Century", 0, 12));
        jLabel2.setText("First Name:");

        txtFirstName.setEditable(false);
        txtFirstName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        txtMiddleName.setEditable(false);
        txtMiddleName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        txtLastName.setEditable(false);
        txtLastName.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        txtContactNo.setEditable(false);
        txtContactNo.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        comboMonth.setMaximumRowCount(12);
        comboMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        comboMonth.setEnabled(false);
        comboMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboMonthActionPerformed(evt);
            }
        });

        txtYear.setEditable(false);
        txtYear.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        txtYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtYearActionPerformed(evt);
            }
        });

        comboDay.setMaximumRowCount(10);
        comboDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        comboDay.setEnabled(false);

        radiobtnMale.setBackground(new java.awt.Color(204, 204, 204));
        radiobtnMale.setSelected(true);
        radiobtnMale.setText("Male");
        radiobtnMale.setEnabled(false);
        radiobtnMale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobtnMaleActionPerformed(evt);
            }
        });

        radiobtnFemale.setBackground(new java.awt.Color(204, 204, 204));
        radiobtnFemale.setText("Female");
        radiobtnFemale.setEnabled(false);
        radiobtnFemale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radiobtnFemaleActionPerformed(evt);
            }
        });

        comboCivil.setMaximumRowCount(5);
        comboCivil.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Single", "Married" }));
        comboCivil.setEnabled(false);

        jLabel9.setFont(new java.awt.Font("Century", 0, 12));
        jLabel9.setText("Civil Status:");

        jLabel8.setFont(new java.awt.Font("Century", 0, 12));
        jLabel8.setText("Gender:");

        jLabel7.setFont(new java.awt.Font("Century", 0, 12));
        jLabel7.setText("Birthdate:");

        jLabel6.setFont(new java.awt.Font("Century", 0, 12));
        jLabel6.setText("Contact No.:");

        jLabel5.setFont(new java.awt.Font("Century", 0, 12));
        jLabel5.setText("Address:");

        jLabel4.setFont(new java.awt.Font("Century", 0, 12));
        jLabel4.setText("Last Name:");

        jLabel3.setFont(new java.awt.Font("Century", 0, 12));
        jLabel3.setText("Middle Name:");

        ToggleEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonEdit.png"))); // NOI18N
        ToggleEdit.setPreferredSize(new java.awt.Dimension(66, 29));
        ToggleEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ToggleEditMouseClicked(evt);
            }
        });
        ToggleEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ToggleEditActionPerformed(evt);
            }
        });

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.setEnabled(false);
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonBack.png"))); // NOI18N
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnPatientLog.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonPatientLog.png"))); // NOI18N
        btnPatientLog.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPatientLogActionPerformed(evt);
            }
        });

        txtAddress.setEditable(false);
        txtAddress.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel2)
                                .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(ToggleEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel5))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(radiobtnMale)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(radiobtnFemale))
                                    .addComponent(comboCivil, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtAddress)
                                    .addComponent(txtLastName)
                                    .addComponent(txtFirstName)
                                    .addComponent(txtMiddleName, javax.swing.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                                    .addComponent(txtContactNo)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(btnPatientLog, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMiddleName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtContactNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(radiobtnMale, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(radiobtnFemale)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(comboCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ToggleEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBack, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPatientLog, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void radiobtnMaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobtnMaleActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_radiobtnMaleActionPerformed

    private void radiobtnFemaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radiobtnFemaleActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_radiobtnFemaleActionPerformed

    private void ToggleEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ToggleEditMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_ToggleEditMouseClicked

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        if (btnBack.getIcon().toString().contains("images/module/ButtonBack.png")) {
            dispose();
            new MainWindow(1).setVisible(true);
        }
        if (btnBack.getIcon().toString().contains("images/module/ButtonCancel.png")) {
            btnConfirm.setEnabled(false);
            txtFirstName.setEditable(false);
            txtMiddleName.setEditable(false);
            txtLastName.setEditable(false);
            txtAddress.setEditable(false);
            txtContactNo.setEditable(false);
            txtYear.setEditable(false);
            comboMonth.setEnabled(false);
            comboDay.setEnabled(false);
            comboCivil.setEnabled(false);
            radiobtnMale.setEnabled(false);
            radiobtnFemale.setEnabled(false);
            ToggleEdit.setSelected(false); //
            btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource(
                    "/images/module/ButtonBack.png")));
        }
    }//GEN-LAST:event_btnBackActionPerformed

    private void btnPatientLogActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPatientLogActionPerformed
        dispose();
        new ViewPatientLogWindow(sPatient).setVisible(true);
 
    }//GEN-LAST:event_btnPatientLogActionPerformed

    // UPDATE PATIENT INFORMATION :)
private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed
        
        JFrame frame = new JFrame();
        int selection = JOptionPane.showConfirmDialog(frame, "Are all the fields correct?",
                "Confirmation", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
         if(selection==JOptionPane.YES_OPTION){
              int errCtr = 0;
              
              String firstName = "\"" + txtFirstName.getText() + "\"" ;
              String lastName = "\"" + txtLastName.getText() + "\"" ;
              String middleName = "\"" + txtMiddleName.getText() + "\"" ;
              String address = "\"" + txtAddress.getText() + "\"";
             
              String birthMonth = "\"" + comboMonth.getSelectedItem().toString() +"\"";
              String civil = "\"" + comboCivil.getSelectedItem().toString() + "\"";
               
              String contactNo = "\""+txtContactNo.getText()+"\"";
              
              //Check if Contact Number is a number
              try{
              long cn = Long.parseLong(txtContactNo.getText());
              }
              catch (NumberFormatException nfe){
                  errCtr++;
                JOptionPane.showMessageDialog(frame, "Please enter a valid contact number!",
                        "NOTICE!",JOptionPane.PLAIN_MESSAGE);  
              }
              
              String birthYear = "\""+txtYear.getText()+"\"";
              
              //Check if Year is a number
              try{
              int by = Integer.parseInt(txtYear.getText());              
              }
              catch (NumberFormatException nfe){
                  errCtr++;
              JOptionPane.showMessageDialog(frame, "Please enter a number for the year!",
                      "NOTICE!",JOptionPane.PLAIN_MESSAGE);
               }

              int birthDay = Integer.parseInt(comboDay.getSelectedItem().toString());
             
              String gender = "";
              
               if(radiobtnMale.isSelected() == true){
                    gender = "\""+"Male"+"\"";
                    radiobtnFemale.setSelected(false);
               }
               else{
                   gender = "\""+"Female"+"\"";
                   radiobtnMale.setSelected(false);
               }
               
               //====ERROR CHECKING====//
               
               if(txtFirstName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter first name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              if(txtLastName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter last name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              if(txtMiddleName.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter middle name!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              if(txtAddress.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter address!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
 
              if(txtContactNo.getText().length() == 0){
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter a valid contact number!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
    
              if(txtYear.getText().length() == 0 || txtYear.getText().length() != 4){        
                  errCtr++;
                   JOptionPane.showMessageDialog(frame, "Please enter a valid year!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
              
              //====ERROR CHECKING====//
                
               if(errCtr == 0){
               try{
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                boolean result = false;

                PreparedStatement pstmt = conn.prepareStatement("UPDATE dcds.patient SET pLastName = "+lastName+ ", pFirstName = "+firstName+", pMiddleName = "+middleName+", pAddress = "+address+", pCivilStat = "+civil+", pBirthMonth = "+birthMonth+", "
                   + "pBirthDay = "+birthDay+", pContactNo = "+contactNo+", pBirthYear = "+birthYear+", pGender = "+gender+" WHERE patientID = "+ sPatient+";");
                pstmt.executeUpdate();
                
                JOptionPane.showMessageDialog(frame, "Update successful!", "Notice", JOptionPane.PLAIN_MESSAGE);

                        conn.close();
                        result = true;
            }catch(Exception e){
                e.printStackTrace();
            }
               dispose();
               new MainWindow(1).setVisible(true);
               }
               
              else if (errCtr > 0){
              errCtr = 0;
              }
               
         } //endof ifyesoption
}//GEN-LAST:event_btnConfirmActionPerformed

private void ToggleEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ToggleEditActionPerformed
    btnConfirm.setEnabled(true);
    txtFirstName.setEditable(true);
    txtMiddleName.setEditable(true);
    txtLastName.setEditable(true);
    txtAddress.setEditable(true);
    txtContactNo.setEditable(true);
    txtYear.setEditable(true);
    comboMonth.setEnabled(true);
    comboDay.setEnabled(true);
    comboCivil.setEnabled(true);
    radiobtnMale.setEnabled(true);
    radiobtnFemale.setEnabled(true);
    btnBack.setIcon(new javax.swing.ImageIcon(getClass().getResource(
            "/images/module/ButtonCancel.png")));
}//GEN-LAST:event_ToggleEditActionPerformed

private void comboMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboMonthActionPerformed
 int index = comboMonth.getSelectedIndex()+1;
    if(index == 2){
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
}       else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
        
    } else if(index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 ||index == 12){
        comboDay.setModel(new DefaultComboBoxModel(days31));
    } else 
        comboDay.setModel(new DefaultComboBoxModel(days30));
    
}//GEN-LAST:event_comboMonthActionPerformed

private void txtYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtYearActionPerformed
    int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
}       else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
}//GEN-LAST:event_txtYearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton ToggleEdit;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JButton btnPatientLog;
    private javax.swing.JComboBox comboCivil;
    private javax.swing.JComboBox comboDay;
    private javax.swing.JComboBox comboMonth;
    private javax.swing.ButtonGroup groupGender;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton radiobtnFemale;
    private javax.swing.JRadioButton radiobtnMale;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtContactNo;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMiddleName;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables
    private Connection connection;
    private Statement stat;
    private ResultSet rs;

   
    
    
    
}
